﻿using ForgottenTalez.GameStateManager.ScreenMan;
using ForgottenTalez.Renderers.Interface;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ForgottenTalez.GameStateManager.Screens.ScreenItems
{
    public class Button
    {
        #region Event Handlers
        /* EVENT HANDLERS: VINNIGE INTRO/TUTORIAL OOR HOE OM DIT TE GEBRUIK (Of een way omit te gebruik in elk geval)
         * 
         * DELEGATE gee die vorm van methods wat aan event haak bv. customMethod(object sender)
         * CustomMethod kan dan aan die EVENT haak soos volg: "button1.selectedEvent += customMethod;" 
         * met button1 'n instance van Button, as Button dan sy selectedEvent roep: "selectedEvent(this);"
         * dan word al die customMethods wat aan die event gehaak is ook geroep en die instance van button
         *  word as argument deur gegee.
         *  
         * In die geval van Button, gaan die update Input gebruik om te bepaal of die button gedruk is en dan die event roep, kyk in button se subklasse.
         * 
         * p.s. Die delegate se vorm kan dalk verander nadat die comment geskryf is so dit kan bv verander na selectedEventHandler(object sender, ...)
         */
        public delegate void selectedEventHandler(Button sender);
        public event selectedEventHandler selectedEvent;
        #endregion

        #region Fields
        Vector2 position;
        Vector2 origin;
        string text;
        bool drawText;
        bool isVisible;
        Vector2 size;
        IButtonRenderer renderer;
        #endregion

        #region Properties
        

        public Rectangle BoundingBox
        {
            get { return new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); }
        }
        public Rectangle Bounds
        {
            get { return new Rectangle(0, 0, (int)size.X, (int)size.Y); }
        }

        public Vector2 Position
        {
            get
            { return this.position; }
            set
            { this.position = value; }
        }

        public Vector2 Origin
        {
            get
            { return this.origin; }
            set
            { this.origin = value; }
        }

        public bool DrawText
        {
            get { return this.drawText; }
            set { this.drawText = value; } 
        }

        public bool IsVisible
        {
            get { return this.isVisible; }
            set { this.isVisible = IsVisible; }
        }

        public string Text
        {
            get { return this.text; }
            set { this.text = value; }
        }
        #endregion

        #region <<<Constructors>>>
        public Button(IButtonRenderer Renderer) : this(Vector2.Zero, Renderer, Vector2.One) { }

        public Button(Vector2 Position, IButtonRenderer Renderer, Vector2 Size) : this(Position, Renderer, "", false, Size) { }

        public Button(Vector2 Position, IButtonRenderer Renderer, string Text, Vector2 Size) : this(Position, Renderer, Text, true, Size) { }

        public Button(Vector2 Position, IButtonRenderer Renderer, string Text, bool DrawText, Vector2 Size) : this(Position, Renderer, Text, true, true, Size) { }

        public Button(Vector2 Position, IButtonRenderer Renderer, string Text, bool DrawText, bool IsVisible, Vector2 Size)
        {
            this.position = Position;
            this.renderer = Renderer;
            this.text = Text;
            this.drawText = DrawText;
            this.isVisible = IsVisible;
            this.size = Size;
        }
        #endregion

        #region Public Methods
        #endregion

        #region Protected Methods
        public void Select()
        {
            selectedEvent(this);
        }
        #endregion

        #region Update and Draw
        public void Update(GameScreen screen, bool isSelected, GameTime gametime)
        {
            if (false /*touched*/)  //Rectangle.Intersect/s
            {
                Select();
            }

        }

        public void Draw(GameScreen screen, bool isSelected, GameTime gameTime)
        {
            renderer.Render(screen, isSelected, gameTime, position, size);

            if (drawText)
            {
                if (isSelected)
                {
                    Color color = Color.Blue;
                    color *= screen.TransitionAlpha;
                    Vector2 origin = new Vector2(0, screen.ScreenManager.Font.LineSpacing);

                    //Draw Text
                    screen.ScreenManager.SpriteBatch.DrawString(screen.ScreenManager.Font,
                        text,
                        position,
                        color,
                        0,
                        origin,
                        0.625f,
                        SpriteEffects.None,
                        0);
                }
                else
                {
                    Color color = Color.Green;
                    color *= screen.TransitionAlpha;
                    Vector2 origin = new Vector2(0, screen.ScreenManager.Font.LineSpacing);

                    //Draw Text
                    screen.ScreenManager.SpriteBatch.DrawString(screen.ScreenManager.Font,
                        text,
                        position,
                        color,
                        0,
                        origin,
                        0.5f,
                        SpriteEffects.None,
                        0);
                }
            }
        }
        #endregion
    }
}
